﻿using System;
using umbraco;

namespace SS4U.Section.Configuration
{
    public partial class CreateSearchDefinition : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            sbmt.Text = ui.Text("create");
        }

        protected void sbmt_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                int nodeId = -1;

                if (helper.Request("nodeId") != "init")
                    nodeId = int.Parse(helper.Request("nodeId"));

                string returnUrl = umbraco.presentation.create.dialogHandler_temp.Create(
                    helper.Request("nodeType"),
                    nodeId,
                    rename.Text);

                if (returnUrl == "")
                {
                    if (nodeId > 0)
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "refresh", "<script>\ntop.refreshTree(true, true);\ntop.closeModal();</script>");
                    else
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "refresh", "<script>\ntop.refreshTree(false, true);\ntop.closeModal();</script>");
                }
                else
                {
                    if (nodeId > 0)
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "refresh", "<script>\ntop.right.location.href = '" + returnUrl + "';\ntop.refreshTree(true, true);\ntop.closeModal();</script>");
                    else
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "refresh", "<script>\ntop.right.location.href = '" + returnUrl + "';\ntop.refreshTree(false, true);\ntop.closeModal();</script>");
                }
            }
        }
    }
}